Author: SAI K
Core Java | Java Tutorial
The continue
statement in Java is a control flow statement that is used to skip the current
iteration of a loop and proceed to the next iteration. It is commonly used within for
,
while
, and do-while
loops to control the flow of the loop based on certain
conditions.
The continue
statement is used to skip the remainder of the code in the current iteration of a
loop and immediately proceed to the next iteration. This is particularly useful when you want to skip
certain iterations based on specific conditions.
continue;
continue
statement is encountered, the remaining code in the loop's current
iteration is skipped.for
loop, the update expression is executed before the next iteration.while
and do-while
loops, the condition is re-evaluated before the next
iteration.
public class SimpleContinueExample {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
if (i == 3) {
continue;
}
System.out.println("i: " + i);
}
}
}
Explanation: This loop prints the numbers 1, 2, 4, and 5. When i
equals 3, the
continue
statement skips the remainder of the loop body, so 3 is not printed.
public class ContinueInForLoop {
public static void main(String[] args) {
for (int i = 1; i <= 10; i++) {
if (i % 2 == 0) {
continue;
}
System.out.println("Odd number: " + i);
}
}
}
Explanation: This loop prints only the odd numbers between 1 and 10 by skipping the even numbers.
public class ContinueInWhileLoop {
public static void main(String[] args) {
int i = 1;
while (i <= 10) {
if (i % 2 == 0) {
i++;
continue;
}
System.out.println("Odd number: " + i);
i++;
}
}
}
Explanation: This loop prints only the odd numbers between 1 and 10 by skipping the even numbers.
public class ContinueInDoWhileLoop {
public static void main(String[] args) {
int i = 1;
do {
if (i % 2 == 0) {
i++;
continue;
}
System.out.println("Odd number: " + i);
i++;
} while (i <= 10);
}
}
Explanation: This loop prints only the odd numbers between 1 and 10 by skipping the even numbers.
public class ContinueInNestedLoops {
public static void main(String[] args) {
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 3; j++) {
if (j == 2) {
continue;
}
System.out.println("i: " + i + ", j: " + j);
}
}
}
}
Explanation: This loop skips the inner loop's iteration when j
equals 2, so it does not print
pairs where j
is 2.
public class ContinueWithLabel {
public static void main(String[] args) {
outerLoop: for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 3; j++) {
if (j == 2) {
continue outerLoop;
}
System.out.println("i: " + i + ", j: " + j);
}
}
}
}
Explanation: This loop uses a label outerLoop
and continues the outer loop when j
equals 2, skipping to the next iteration of the outer loop.
The continue
statement in Java is a useful control flow tool that allows you to skip the current
iteration of a loop and proceed to the next iteration. Understanding how to use continue
effectively, including within nested loops and with labels, can help you write more efficient and readable
code.